<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>评论列表</title>
  <link rel="stylesheet" href="./css/bootstrap.min.css">
  <style>
    .badge {
      float: right;
      margin-right: 5px;
    }

    .my-page {
      margin: 0 5px;
    }

    .all-page-content {
      margin-left: 5px;
    }
  </style>
</head>

<body style="padding: 15px;">

  <!-- 评论面板 -->
  <div class="panel panel-primary">
    <div class="panel-heading">
      <h3 class="panel-title">发表评论</h3>
    </div>
    <form class="panel-body cmt-form">
      <div>评论人：</div>
      <input type="text" class="form-control" name="username" autocomplete="off" />
      <div>评论内容：</div>
      <textarea class="form-control" name="content"></textarea>
      <button type="submit" class="btn btn-primary submit">发表评论</button>
    </form>
  </div>


  <!-- 评论列表 -->
  <ul class="list-group">
    <!-- <li class="list-group-item">
       <span>评论内容</span>
       <span class="badge del" style="cursor:pointer; background-color: lightgray;">删除</span> 
       <span class="badge" style="background-color: #F0AD4E;">评论时间：xxx</span>
       <span class="badge" style="background-color: #5BC0DE;">评论人：xxx</span>
    </li> -->
  </ul>

  <!-- 分页器 -->
  <nav>
    <ul class="pagination">
      <li>
        <button class="last">
          <span>&laquo;</span>
        </button>
      </li>
      <li class="my-page">
        <span class="page-show"></span>
      </li>
      <li>
        <button class="next">
          <span>&raquo;</span>
        </button>
      </li>
      <li class="all-page-content">
        <span>共计:<span class="all-page"></span>页</span>
      </li>
    </ul>
  </nav>
  <script src="../../JS/axios.min.js"></script>
  <script src="./lib/form-serialize.js"></script>
  <script>
    // 获取评论
    let cPages = 1
    // 最大页码
    let maxPage = null
    // 定义获取评论函数 形参页数
    async function getTe(page) {
      const res = await axios({
        url: 'http://123.57.109.30:3006/api/cmtlist',
        params: {
          page
        }
      })
      document.querySelector('.list-group').innerHTML = res.data.data.map(item => `<li data-id="${item.id}" class="list-group-item">
       <span>${item.content}</span>
       <span data-id="${item.id}" class="badge del" style="cursor:pointer; background-color: lightgray;">删除</span>
       <span class="badge" style="background-color: #F0AD4E;">评论时间：${item.time}</span>
       <span class="badge" style="background-color: #5BC0DE;">评论人：${item.username}</span>
      </li>`).join('')
      document.querySelector('.all-page').innerHTML = res.data.allPage
      document.querySelector('.page-show').innerHTML = cPages
      maxPage = res.data.allPage
    }
    getTe(cPages)



    // 发表新评论功能
    document.querySelector('.cmt-form').addEventListener('submit', async function (e) {
      e.preventDefault()
      const data = serialize(this, { hash: true, empty: true })
      await axios({
        url: 'http://123.57.109.30:3006/api/addcmt',
        method: 'post',
        data
      })
      getTe(cPages)
    })

    // 删除评论功能
    document.querySelector('.list-group').addEventListener('click', async function (e) {
      if (e.target.classList.contains('del')) {
        let id = e.target.dataset.id
        await axios({
          url: 'http://123.57.109.30:3006/api/delcmt',
          params: {
            id
          }
        })
        getTe(cPages)
      }
    })

    // 上下页按钮功能
    document.querySelector('.last').addEventListener('click', function () {
      if (cPages === 1) {
        return
      }
      cPages--
      getTe(cPages)
    })

    document.querySelector('.next').addEventListener('click', function () {
      if (cPages === maxPage) {
        return
      }
      cPages++
      getTe(cPages)
    })
    // document.querySelector('.pagination').addEventListener('click', function (e) {
    //   if (e.target.classList.contains('next')) {
    //     cPages++
    //     getTe()
    //     document.querySelector('.pagination .last').disabled = false
    //   }
    //   if (e.target.classList.contains('last')) {
    //     cPages--
    //     getTe()
    //     if (cPages <= 1) {
    //       document.querySelector('.pagination .last').disabled = true
    //     }
    //   }
    // })

  </script>
</body>

</html>